package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.menuitem;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilConstants;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilEntityType;
import edu.stanford.bmir.protege.web.client.ui.library.msgbox.MessageBox;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.treepanel.AbstractStkosTreePanel;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.StkosCutterConstant;
import edu.stanford.bmir.protege.web.shared.entity.OWLEntityData;

public class CancelCutterRecordMenuItem extends
		AbstractStkosCutterTreeNodeMenuItem {

	public static final String TEXT = "取消裁切当前条目";
	public static final String ICONCLS = StkosCutterConstant.CANCEL_CUTTER_ICONCLS;

	public CancelCutterRecordMenuItem(TreeNode selectedTreeNode,
			Project project, AbstractStkosTreePanel treePanel) {
		super(TEXT, ICONCLS, selectedTreeNode, project, treePanel);
	}

	@Override
	public void onClick(BaseItem item, EventObject event) {
		removeAnnotationValue();
	}

	protected void removeAnnotationValue() {
		String entityName = null;
		if (selectedTreeNode.getUserObject() instanceof EntityData) {
			entityName = ((EntityData) selectedTreeNode.getUserObject())
					.getName();
		} else if (selectedTreeNode.getUserObject() instanceof OWLEntityData) {
			entityName = ((OWLEntityData) selectedTreeNode.getUserObject())
					.getEntity().getIRI().toString();
		} else {
			MessageBox.showAlert("提示", "必要数据不完整，无法执行操作!");
			return;
		}
		checkNull(entityName);

		String projectName = project.getProjectId().getId();
		UtilEntityType entityType = UtilEntityType.ANY_ENTITY;
		String annotaionName = "rdfs:comment";
		String annotaionValuePattern = UtilConstants.FLAG_CUTTING + ":*";
		AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {

			@Override
			public void onFailure(Throwable caught) {
				MessageBox.showErrorMessage(TEXT + "失败", caught);
			}

			@Override
			public void onSuccess(Integer result) {
				refreshSelectedTreeNode();
			}
		};
		
		removeAnnotationValue(projectName, entityName, entityType,
				annotaionName, annotaionValuePattern, callback);
	}
}
